// pages/order/orderList/orderList.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list: [],
    page: 1,
    loadmoreText: '加载更多数据...'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData('update');
  },
  /**
   * 改变状态
   */
  changeStatus(e) {
    this.setData({ active: e.currentTarget.dataset.index, page: 1 });
    this.getData('update');
  },
  /**
   * 监听上拉加载
   */
  onReachBottom() {
    this.setData({ page: this.data.page + 1 });
    this.getData('loadmore');
  },
  /**
   * 获取数据
   */
  getData(type) {
    app.request('/Order/QuotationList', 'GET', {
      userId: app.globalData.userId,
      type: this.data.active,
      page: this.data.page,
      pageSize: 10
    }, res => {
      if (type == 'update') {
        this.setData({ list: [] });
        wx.stopPullDownRefresh();
      }

      res.list.map(item => {
        item.quotationTypeText = item.quotationType === 1 ? "保险代缴" : "年审预约";
        return item;
      });
      var newList = this.data.list.concat(res.list);
      var loadmoreText = res.list.length < 10 ? '没有更多数据了' : '加载更多数据...';
      this.setData({ list: newList, loadmoreText: loadmoreText });
    });
  },
  /**
   * 点击取消报价单
   */
  cancelFn(e) {
    let dataset = e.currentTarget.dataset;
    wx.showModal({
      title: '取消报价单',
      content: `确定要取消此${dataset.type ? '保险' : '年审'}报价单吗？`,
      success: res => {
        if (res.confirm) {
          app.request('/Order/CancelQuotation', 'DELETE', {
            userId: app.globalData.userId,
            quotationType: dataset.type,
            quotationId: dataset.id
          }, res => {
            let list = this.data.list;
            list.splice(dataset.index, 1);
            this.setData({ list: list });
            wx.showToast({ title: '取消成功', icon: 'none', duration: 2000 });
          });
        }
      }
    });
  }
})